/**
 * @author: 王志昂
 * @create: 2014年6月5日
 * @note: 前端JS公共模块
 * @history:
 * 2014年6月5日 11:39 创建
 */
define(function (require, exports, module) {
    require('jquery');
    require('underscore');
    require("pop")($);
    require("select2")($);
    var ApiUtil = {
        refreshApiCateList: function () {
            window.location.reload();
        },
        bindAddApiCateButtonEvent: function () {
            $('#addApiCateBtn').click(function () {
                $('#add_api_cate_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        bindEditApiCateButtonEvent: function () {
            var self = this;
            $(document).on('click', '.editBtn', function () {
                var id = $(this).siblings('.cate_id').val();
                $.ajax({
                    url: domain + '/home/api/get_api_cate?id=' + id,
                    method: 'GET',
                    dataType: 'json',
                    success: function (data) {
                        if (data.errorCode == 0) {
                            $('#add_api_cate_window_id').val(data.data.aid);
                            $('#add_api_cate_window_name').val(data.data.cname);
                            $('#add_api_cate_window_desc').val(data.data.cdesc);
                            $('#add_api_cate_window').popup({
                                filter: '#mask',
                                zindex: 9990
                            });
                        }
                    }
                });
            });
        },
        bindAddApiCateWindowCloseEvent: function () {
            var self = this;
            $('#add_api_cate_window').find('.remove').click(function () {
                $('#add_api_cate_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_api_cate_window_cancel_btn').click(function () {
                $('#add_api_cate_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindSaveApiCateButtonEvent: function (callback) {
            var self = this;
            $('#add_api_cate_window_save_btn').click(function () {
                var id = $('#add_api_cate_window_id').val();
                var name = $('#add_api_cate_window_name').val();
                var desc = $('#add_api_cate_window_desc').val();
                var parentId = $('#parentId').val();

                $.ajax({
                    url: domain + '/home/api/save_api_cate',
                    method: 'POST',
                    dataType: 'json',
                    data: {
                        id: id,
                        name: name,
                        desc: desc,
                        parentId: parentId
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_api_cate_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        }
    };
    module.exports = ApiUtil;
});
